package com.suxuantech.light_image

//import io.flutter.embedding.android.FlutterActivity

import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.Signature
import androidx.annotation.NonNull
import cn.smssdk.flutter.MobsmsPlugin
import com.mob.MobSDK
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException


class MainActivity : FlutterActivity() {
    override fun onDestroy() {
        super.onDestroy()
        // 执行回收操作
        MobsmsPlugin.recycle()
    }
    //通讯名称,回到手机桌面
    private val CHANNEL = "android/back/desktop"

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        // 注册SMSSDK Flutter插件
//        MobsmsPlugin.registerWith(registrarFor(MobsmsPlugin.CHANNEL));
        // 初始化SMSSDK
        MobSDK.init(this, "185afd2d1b380", "fa6c235598dc50b9b7881680b84109a8");
        android.util.Log.d("md5", getSignMd5Str())
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "app.channel.shared.data").setMethodCallHandler(
                MethodChannel.MethodCallHandler { call, result ->
                    run {
                        if (call.method.contentEquals("getParam")) {
                            result.success("message")
                        }
                    }
                }
        )

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler(
                MethodChannel.MethodCallHandler { methodCall, result ->
                    if (methodCall.method.equals("backDesktop")) {
                        result.success(true);
                        moveTaskToBack(false);
                    }
                }
        );


    }

    /**
     * 获取app签名md5值
     */
    fun getSignMd5Str(): String? {
        try {
            val packageInfo: PackageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES)
            val signs: Array<Signature> = packageInfo.signatures
            val sign: Signature = signs[0]
            return encryptionMD5(sign.toByteArray())
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return ""
    }

    /**
     * MD5加密
     * @param byteStr 需要加密的内容
     * @return 返回 byteStr的md5值
     */
    fun encryptionMD5(byteStr: ByteArray?): String? {
        var messageDigest: MessageDigest? = null
        val md5StrBuff = StringBuffer()
        try {
            messageDigest = MessageDigest.getInstance("MD5")
            messageDigest.reset()
            messageDigest.update(byteStr)
            val byteArray: ByteArray = messageDigest.digest()
            for (i in byteArray.indices) {
                if (Integer.toHexString(0xFF and byteArray[i].toInt()).length == 1) {
                    md5StrBuff.append("0").append(Integer.toHexString(0xFF and byteArray[i].toInt()))
                } else {
                    md5StrBuff.append(Integer.toHexString(0xFF and byteArray[i].toInt()))
                }
            }
        } catch (e: NoSuchAlgorithmException) {
            e.printStackTrace()
        }
        return md5StrBuff.toString()
    }
}
